<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/21
 * Time: 11:10
 */

namespace Home\Controller;


use Think\Controller;

class UserController extends Controller
{
        //@todo 加载登录页面
    public function login(){
        $this->display();
    }

    //@todo 登录操作
    public function loginAction(){
        $name=$_POST['name'];
        $password=$_POST['password'];
        $info=D('user')->where(array('name'=>$name,'password'=>md5($password),'status'=>0))->find();
        if(!$info){
            $this->ajaxReturn(array('success'=>false,'message'=>'用户名密码错误'));
        }
        $_SESSION['user']=array(
            'name'=>$info['name'],
            'id'=>$info['id']
        );
        $this->ajaxReturn(array('success'=>true,'message'=>'登陆成功'));
    }

    //@todo 修改密码
    public function edit_pass(){
        $id=$_POST['id'];
        $password=$_POST['pass'];
        $data=array(
            'id'=>$id,
            'password'=>md5($password)
        );
        $res=D('user')->save($data);
        if(!is_numeric($res)){
            $this->ajaxReturn(array('success'=>false,'message'=>'密码修改失败,请稍后再试'));
        }
        unset($_SESSION['user']);
        $this->ajaxReturn(array('success'=>true,'message'=>'密码修改成功,请重新登录'));

    }

    //@todo 退出
    public function logout(){
        unset($_SESSION['user']);
        $this->ajaxReturn(array('success'=>true,'message'=>'成功退出'));
    }
}